package com.example.foobar.controller;

import com.example.foobar.business.service.DaRetentionRatesService;
import com.example.foobar.dto.RetentionRateDTO;
import com.example.foobar.entity.DaRetentionRates;
import com.example.foobar.param.DaRetentionRatesPageParam;
import com.example.foobar.vo.RetentionRateVO;
import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.controller.BaseController;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import io.geekidea.springbootplus.framework.core.validator.groups.Add;
import io.geekidea.springbootplus.framework.core.validator.groups.Update;
import io.geekidea.springbootplus.framework.log.annotation.Module;
import io.geekidea.springbootplus.framework.log.annotation.OperationLog;
import io.geekidea.springbootplus.framework.log.enums.OperationLogType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;

/**
 * 留存率记录 控制器
 *
 * @author 郑英奔
 * @since 2021-06-05
 */
@Slf4j
@RestController
@RequestMapping("/daRetentionRates")
@Module("foobar")
@Api(value = "留存率记录API", tags = {"留存率记录"})
public class DaRetentionRatesController extends BaseController {

    @Autowired
    private DaRetentionRatesService daRetentionRatesService;

    /**
     * 添加留存率记录
     */
    @PostMapping("/add")
    @OperationLog(name = "添加留存率记录", type = OperationLogType.ADD)
    @ApiOperation(value = "添加留存率记录", response = ApiResult.class)
    @RequiresPermissions("sys:qr-rate:page")
    public ApiResult<Boolean> addDaRetentionRates(@Validated(Add.class) @RequestBody DaRetentionRates daRetentionRates) throws Exception {
        boolean flag = daRetentionRatesService.saveDaRetentionRates(daRetentionRates);
        return ApiResult.result(flag);
    }

    /**
     * 修改留存率记录
     */
    @PostMapping("/update")
    @OperationLog(name = "修改留存率记录", type = OperationLogType.UPDATE)
    @ApiOperation(value = "修改留存率记录", response = ApiResult.class)
    @RequiresPermissions("sys:qr-rate:page")
    public ApiResult<Boolean> updateDaRetentionRates(@Validated(Update.class) @RequestBody DaRetentionRates daRetentionRates) throws Exception {
        boolean flag = daRetentionRatesService.updateDaRetentionRates(daRetentionRates);
        return ApiResult.result(flag);
    }

    /**
     * 删除留存率记录
     */
    @PostMapping("/delete/{id}")
    @OperationLog(name = "删除留存率记录", type = OperationLogType.DELETE)
    @ApiOperation(value = "删除留存率记录", response = ApiResult.class)
    @RequiresPermissions("sys:qr-rate:page")
    public ApiResult<Boolean> deleteDaRetentionRates(@PathVariable("id") Long id) throws Exception {
        boolean flag = daRetentionRatesService.deleteDaRetentionRates(id);
        return ApiResult.result(flag);
    }

    /**
     * 获取留存率记录详情
     */
    @GetMapping("/info/{id}")
    @OperationLog(name = "留存率记录详情", type = OperationLogType.INFO)
    @RequiresPermissions("sys:qr-rate:page")
    @ApiOperation(value = "留存率记录详情", response = DaRetentionRates.class)
    public ApiResult<DaRetentionRates> getDaRetentionRates(@PathVariable("id") Long id) throws Exception {
        DaRetentionRates daRetentionRates = daRetentionRatesService.getById(id);
        return ApiResult.ok(daRetentionRates);
    }

    /**
     * 留存率记录分页列表
     */
    @PostMapping("/getPageList")
    @RequiresPermissions("sys:qr-rate:page")
    @OperationLog(name = "留存率记录分页列表", type = OperationLogType.PAGE)
    @ApiOperation(value = "留存率记录分页列表", response = DaRetentionRates.class)
    public ApiResult<Paging<DaRetentionRates>> getDaRetentionRatesPageList(@Validated @RequestBody DaRetentionRatesPageParam daRetentionRatesPageParam) throws Exception {
        Paging<DaRetentionRates> paging = daRetentionRatesService.getDaRetentionRatesPageList(daRetentionRatesPageParam);
        return ApiResult.ok(paging);
    }

    /**
     * 留存率记录分页列表
     */
    @GetMapping("/getList")
    @RequiresPermissions("sys:qr-rate:page")
    @OperationLog(name = "查询历史留存率", type = OperationLogType.PAGE)
    @ApiOperation(value = "查询历史留存率", response = DaRetentionRates.class)
    public ApiResult<Paging<RetentionRateVO>> getList(String uniacid, Date startDate, Date endDate, int dayCount) {
        ZonedDateTime zonedDateTime = LocalDate.now().atStartOfDay(ZoneId.systemDefault());
        Date today =Date.from(zonedDateTime.toInstant());
        Paging<RetentionRateVO> paging = daRetentionRatesService.getStroyRetentonRates(uniacid, startDate, today, dayCount);
        return ApiResult.ok(paging);
    }

    /**
     * 留存率记录分页列表
     */
    @GetMapping("/pullActiveDate")
    @OperationLog(name = "拉取某一天的数据")
    @ApiOperation(value = "拉取某一天数据")
    public void pullActiveDate(Date date) {
        daRetentionRatesService.pullActiveDateYingma(date);
    }

}

